{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 53. Maximum Subarray 最大子序和\n",
    "\n",
    "### 难度：Medium\n",
    "\n",
    "## 刷题内容\n",
    "\n",
    "> 原题链接\n",
    "\n",
    " - 中文：https://leetcode-cn.com/problems/maximum-subarray/description/\n",
    " - 英文：https://leetcode.com/problems/maximum-subarray/\n",
    " \n",
    "> 内容描述\n",
    "\n",
    "```\n",
    "给定一个整数数组 nums ，找到一个具有最大和的连续子数组（子数组最少包含一个元素），返回其最大和。\n",
    "\n",
    "示例：\n",
    "\n",
    "输入: [-2,1,-3,4,-1,2,1,-5,4],\n",
    "输出: 6\n",
    "解释: 连续子数组 [4,-1,2,1] 的和最大，为 6。\n",
    "\n",
    "进阶：\n",
    "如果你已经实现复杂度为 O(n) 的解法，尝试使用更为精妙的分治法求解。\n",
    "```\n",
    "\n",
    "## 解题方案\n",
    "\n",
    "> 思路 1\n",
    "\n",
    " - 可以用 O(N^2) 循环\n",
    " - 从i开始，计算i到n，存比较大的sum\n",
    " - 会超时，不会 AC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "class Solution:\n",
    "    def maxSubArray(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        # 设置为无穷小\n",
    "        ans = float('-inf')\n",
    "        # 这个循环控制子串的元素个数\n",
    "        for i in range(1, len(nums)):\n",
    "            # 这个循环控制子串从原始串的哪个位置开始计算\n",
    "            for j in range(len(nums)-i):\n",
    "                big = 0\n",
    "                big = sum(nums[j : j+i])\n",
    "                if big > ans:\n",
    "                    ans = big\n",
    "        return ans\n",
    "\n",
    "s = Solution()\n",
    "nums_1 = [-2,1,-3,4,-1,2,1,-5,4]\n",
    "nums_2 = [1, -1]\n",
    "print(s.maxSubArray(nums_1))\n",
    "print(s.maxSubArray(nums_2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 思路 2\n",
    "\n",
    " - 使用动态规划\n",
    " - ms(i) = max(ms[i-1] + a[i], a[i])\n",
    " - 我们只需要知道，计算到 i 处的最大值的两种可能，一个是加上 a[i]，另一个是从 a[i] 开始重新计算子串。\n",
    " - 比较 ms[i-1]+a[i] 与 a[i] 的值的大小关系，如果前者小于后者，就说明，前面的子串的最大的和是负数，我们可以抛弃掉，而从 a[i] 处开始起头重新计算子串；如果前者大于后者，说明，前面的子串的最大的和是正数，我们可以加上 a[i] 继续计算。\n",
    " - 可以 AC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "class Solution(object):\n",
    "    def maxSubArray(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        n = len(nums)\n",
    "        maxSum = [nums[0] for i in range(n)]\n",
    "        for i in range(1,n):\n",
    "        \tmaxSum[i] = max(maxSum[i-1] + nums[i], nums[i])\n",
    "        return max(maxSum)\n",
    "    \n",
    "s = Solution()\n",
    "nums_1 = [-2,1,-3,4,-1,2,1,-5,4]\n",
    "nums_2 = [1, -1]\n",
    "print(s.maxSubArray(nums_1))\n",
    "print(s.maxSubArray(nums_2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 思路 3\n",
    "\n",
    "Kadane’s Algorithm wikipedia可以查到,然后一般的是负的可以还回0，这里需要稍作修改，参考：\n",
    "http://algorithms.tutorialhorizon.com/kadanes-algorithm-maximum-subarray-problem/\n",
    "\n",
    "```\n",
    "start:\n",
    "    max_so_far = a[0]\n",
    "    max_ending_here = a[0]\n",
    "\n",
    "loop i= 1 to n\n",
    "  (i) max_end_here = Max(arrA[i], max_end_here+a[i]);\n",
    "  (ii) max_so_far = Max(max_so_far,max_end_here);\n",
    "\n",
    "return max_so_far\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "AC 代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "class Solution(object):\n",
    "    def maxSubArray(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        n = len(nums)\n",
    "        maxSum , maxEnd = nums[0], nums[0]\n",
    "        \n",
    "        for i in range(1,n):\n",
    "            maxEnd = max(nums[i],maxEnd + nums[i])\n",
    "            maxSum = max(maxEnd,maxSum)\n",
    "        return maxSum\n",
    "    \n",
    "s = Solution()\n",
    "nums_1 = [-2,1,-3,4,-1,2,1,-5,4]\n",
    "nums_2 = [1, -1]\n",
    "print(s.maxSubArray(nums_1))\n",
    "print(s.maxSubArray(nums_2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 思路 4\n",
    "\n",
    "参见clrs 第71页，用divide and conquer，有伪码\n",
    "\n",
    "最大的 subarray sum 有三个可能，左半段或者右半段，或者跨越左右半段,\n",
    "\n",
    "速度比较慢，AC代码，复杂度O(NlogN)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "    def maxSubArray(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        def find_max_crossing_subarray(nums, low, mid, high):\n",
    "            left_sum = float('-inf')\n",
    "            sum = 0\n",
    "            for i in xrange(mid,low-1,-1):\n",
    "                sum = sum + nums[i]\n",
    "                if sum > left_sum:\n",
    "                    left_sum = sum\n",
    "\n",
    "            right_sum = float('-inf')\n",
    "            sum = 0\n",
    "            for j in range(mid+1,high+1):\n",
    "                sum = sum + nums[j]\n",
    "                if sum > right_sum:\n",
    "                    right_sum = sum\n",
    "\n",
    "            return left_sum + right_sum\n",
    "\n",
    "        def find_max_subarray(nums,low,high):\n",
    "            if low == high: \n",
    "                return nums[low]\n",
    "            else:\n",
    "                mid = (low + high) / 2\n",
    "                left_sum = find_max_subarray(nums, low, mid)\n",
    "                right_sum = find_max_subarray(nums,mid+1,high)\n",
    "                cross_sum = find_max_crossing_subarray(nums,low,mid,high)\n",
    "                # print left_sum, right_sum, cross_sum\n",
    "                # print mid, low, high\n",
    "                return max(left_sum, right_sum, cross_sum)\n",
    "\n",
    "        return find_max_subarray(nums, 0, len(nums)-1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
